package ualrcdg.shared;

import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ualrcdg.client.R;
import android.content.Context;


public class LayoutParser
{
	public void parseLayout( Context context )
	{
		try
		{
			final String ANDROID_ID = "android:id";
			String createprofilelayout = "createprofilelayout";
			String layoutFile = "/data/data/res/layout/" + createprofilelayout + ".xml";
			InputStream in = context.getResources( ).openRawResource( R.raw.createprofilelayout );
			//File fXmlFile = new File(context.getResources().openRawResource(R.raw.chunk));
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance( );
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder( );
			Document doc = dBuilder.parse( in );
			doc.getDocumentElement( ).normalize( );

			NodeList nList = doc.getElementsByTagName( "Button" );

			for( int temp = 0; temp < nList.getLength( ); temp++ )
			{
				Node nNode = nList.item( temp );
				if( nNode.getNodeType( ) == Node.ELEMENT_NODE )
				{
					Element eElement = ( Element ) nNode;

					if( eElement.hasAttribute( ANDROID_ID ) ) System.out.println( "ID: " + eElement.getAttribute( ANDROID_ID ) );

				}
			}

			NodeList nList1 = doc.getElementsByTagName( "TextView" );

			for( int temp = 0; temp < nList1.getLength( ); temp++ )
			{
				Node nNode = nList1.item( temp );
				if( nNode.getNodeType( ) == Node.ELEMENT_NODE )
				{
					final Element eElement = ( Element ) nNode;

					if( eElement.hasAttribute( ANDROID_ID ) ) System.out.println( "ID: " + eElement.getAttribute( ANDROID_ID ) );

				}
			}

			NodeList nList2 = doc.getElementsByTagName( "EditText" );

			for( int temp = 0; temp < nList2.getLength( ); temp++ )
			{
				Node nNode = nList2.item( temp );
				if( nNode.getNodeType( ) == Node.ELEMENT_NODE )
				{
					Element eElement = ( Element ) nNode;

					if( eElement.hasAttribute( ANDROID_ID ) ) System.out.println( "ID: " + eElement.getAttribute( ANDROID_ID ) );

				}
			}

			NodeList nList3 = doc.getElementsByTagName( "LinearLayout" );

			for( int temp = 0; temp < nList3.getLength( ); temp++ )
			{
				Node nNode = nList3.item( temp );
				if( nNode.getNodeType( ) == Node.ELEMENT_NODE )
				{
					Element eElement = ( Element ) nNode;

					if( eElement.hasAttribute( ANDROID_ID ) ) System.out.println( "ID: " + eElement.getAttribute( ANDROID_ID ) );

				}
			}

		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
	}
}
